import axios from 'axios'
// import { Toast } from 'vant'
import { getItem, key, removeItem } from '@/utils/storage'
import router from '@/router'

const request = axios.create({
  baseURL: 'http://liufusong.top:8080',
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use((config) => {
  console.log('config', config)
  const token = getItem(key)
  config.data.body = `application/json/${token}`
  return config
}, err => {
  return Promise.reject(err)
})
// 响应拦截器
request.interceptors.request.use((resopnse) => {
  console.log('resopnse', resopnse)
  return resopnse
}, err => {
  console.log('err', err)
  if (err.resopnse.data.status === 401) {
    removeItem(key)
    router.push('/login')
  }
  // Toast(err.data.description)
  return Promise.reject(err)
})

export default request
